/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.streamline.services;

import com.vii.streamline.structures.Selector;
import com.vii.streamline.structures.collections.InnerList;

import javax.script.ScriptException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Service class to handle iterator-related operations
 */
public class IteratorServices {

    /**
     * Select a subset of elements from a collection by a regular expression
     * @param collection source collection of the elements
     * @param pattern selector regular pattern
     * @param <T> generic type of elements
     * @return selected elements, subset of the original collection
     */
    public static <T> Iterable<T> selectByRegular( Iterable<T> collection, Pattern pattern){
        ArrayList<T> l = new ArrayList<T>();
        for( T t : collection )
            if( RegularServices.matches(t.toString(), pattern) )
                l.add( t );
        return l;    
    }

    /**
     * Select a subset of elements from a collection by a regular expression
     * @param collection source collection of the elements
     * @param regularExpression selector regular pattern string
     * @param <T> generic type of elements
     * @return selected elements, subset of the original collection
     */
    public static <T> Iterable<T> selectByRegular( Iterable<T> collection, String regularExpression){
        return selectByRegular( collection, RegularServices.registerPattern(regularExpression) );
    }

    /**
     * Select a subset of elements from a collection by a Selector object
     * @param collection source collection of the elements
     * @param selector selector object
     * @param <T> generic type of elements
     * @return selected elements, subset of the original collection
     * @throws ScriptException
     */
    public static <T> Iterable<T> selectBySelector( Iterable<T> collection, Selector<T> selector ){
        ArrayList<T> l = new ArrayList<T>();
        for( T t : collection )
            if( selector.isNeeded( t ) )
                l.add( t );
        return l;
    }

    /**
     * Adds all element from the "collection" to the list
     * @param list list to be extended
     * @param collection collection to be added
     * @param <T> generic type of the elements
     * @return extended list
     */
    public static <T> List<T> collect(List<T> list, Iterable<T> collection ){
        for( T t : collection )
            list.add( t );
        return list;
    }

    /**
     * Linearize a structured array of objects. The array may contains arrays and so on. This method project this structure into a linear array
     * it delegates call to linearizate( LinkedList<Serializable> list, Serializable... params ) method and convert is into array
     */
    public static Serializable[] linearizateAsArray( Serializable... params ){
        List<Serializable> list = linearizate( new LinkedList<Serializable>(), params );
        return list.toArray( new Serializable[list.size()] );
    }

    /**
     * Linearize a structured array of objects. The array may contains arrays and so on. This method project this structure into a linear list
     * it delegates call to linearizate( LinkedList<Serializable> list, Serializable... params ) method
     */
    public static List<Serializable> linearizate( Serializable... params ){
        return linearizate( new LinkedList<Serializable>(), params );        
    }

    /**
     * Linearize a structured array of objects. The array may contains arrays and so on. This method project this structure into a linear list
     * @param list result list where element will be put
     * @param params source structure
     * @return list containing linearized structure
     */
    public static List<Serializable> linearizate( LinkedList<Serializable> list, Serializable... params ){
        for( Serializable t : params ){
            if( t == null ) continue;
            else if( t.getClass().equals( Serializable[].class ) )
                linearizate( list, (Serializable[])t );
            else
                list.add( t );
        }
        return list;
    }


    public static <T> List<T> linearizate( Map<String, List<T>> map ){
        InnerList<T> list = new InnerList<T>();
        
        for( String key : map.keySet() )
            list.add( map.get( key ) );
        
        return list;
    }

}
